package com.ruoyi.listener.improve;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.quality.domain.bo.QualityContinuousImprovementBo;
import com.ruoyi.quality.service.IQualityContinuousImprovementService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Component
public class AddFromListener implements ExecutionListener {

    @Resource
    public IQualityContinuousImprovementService improvementService;

    private static AddFromListener addFromListener;
    @PostConstruct
    public void init(){
        addFromListener = this;
        addFromListener.improvementService = this.improvementService;
    }

    @Override
    public void notify(DelegateExecution delegateExecution) {
        System.out.println("流程启动");
        delegateExecution.getEventName();
        Map<String, Object> variables = delegateExecution.getVariables();
        String procId = delegateExecution.getProcessInstanceId();
        // 创建ObjectMapper对象
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println("流程"+procId);
        // 将Map转换为类对象
        QualityContinuousImprovementBo qualityContImproveBo = objectMapper.convertValue(variables,QualityContinuousImprovementBo.class);
        qualityContImproveBo.setProcessInstanceId(procId);
        Long initiator = Long.valueOf(String.valueOf(variables.get("initiator")));
        qualityContImproveBo.setUserId(initiator);
        String schedule = String.valueOf(variables.get("processStatus"));
        qualityContImproveBo.setSchedule(schedule);
        qualityContImproveBo.setDate(new Date());
        addFromListener.improvementService.insertByBo(qualityContImproveBo);
        System.out.println("流程结束");
    }
}
